package com.sz.ac.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.sz.ac.entity.AcGrp;
import com.sz.ac.entity.AcInvest;
import com.sz.ac.interfs.IAcGrpService;
import com.sz.ac.interfs.IAcInvestService;
import com.sz.ac.model.constant.DefMsgException;
import com.sz.ac.model.constant.ResultData;
import com.sz.ac.services.mapper.ac.AcGrpMapper;
import com.sz.ac.utils.excel.ImportExcel;

@Controller
public class IndexController 
{
	private final Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Resource
	private IAcInvestService acInvestService; 
	@Resource
	private IAcGrpService grpService; 
	
	@RequestMapping(value="/")
	public String index(){
		return "login";
	}
	
//	@PostMapping(value="login",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
//	@ResponseBody
//	public ResultData login(@RequestParam Map<String, String> paramMap)
//	{
//		String userName = paramMap.get("userName");
//		String pwd = paramMap.get("pwd");
//		logger.info("userName={},pwd={}",userName,pwd);
//		
//		
//		return ResultData.success("");
//	}
	
	@PostMapping(value="importExcel")
	@ResponseBody
	public ResultData importExcel(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	{
		try
		{
			request.setCharacterEncoding("UTF-8");
	        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
	        /** 页面控件的文件流* */
	        MultipartFile multipartFile = null;
	        Map map = multipartRequest.getFileMap();
	        for (Iterator i = map.keySet().iterator(); i.hasNext();) 
	        {
	        	Object obj = i.next();
	            multipartFile = (MultipartFile) map.get(obj);
	        }
	        if(multipartFile == null){
	        	return ResultData.error("请上传文件");
	        }
	        /** 获取文件的后缀* */
	        String sourceFile = multipartFile.getOriginalFilename();
			//进一步判断文件是否为空（即判断其大小是否为0或其名称是否为null）
			long size =multipartFile.getSize();
			if (StringUtils.isEmpty(sourceFile) && size==0) return ResultData.error("文件为空");
			 
			  //批量导入。参数：文件名，文件。
			List<AcInvest> list = new ImportExcel().importExcel(sourceFile, multipartFile);
			if(!list.isEmpty())
			{
				acInvestService.importInvestData(list);
				return ResultData.success("导入EXCEL成功");
			}else
			{
				return ResultData.error("导入EXCEL失败");
			}
		}
		catch(DefMsgException e)
		{
			logger.error("===e:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("===e:{}",e);
			return ResultData.error("导入EXCEL异常");
		}
		
	}
	
	/**
	 * @param paramMap
	 * @return
	 * test
	 */
	@GetMapping(value="getAcGrpList",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public ResultData getAcGrpList(){
		try
		{
			
			return ResultData.success(grpService.getAllGrpView());
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	
	/**
	 * @param paramMap
	 * @return
	 * test
	 */
	@GetMapping(value="getAcInvestList",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public ResultData getAcInvestList(){
		try
		{
			
			return ResultData.success(acInvestService.getAllInvest());
		}
		catch(DefMsgException e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error(e.getDefMsg());
		}
		catch(Exception e)
		{
			logger.error("==ex:{}",e);
			return ResultData.error();
		}
	}
	
}
